
function addComment(){
	var newsId=$("#newsId").val();
	var words=filter($("#newsWords").val());
	if(words==null || words.replace(/\s/g, "")==""){
		layer.alert("评论信息不能为空",8);
		$('.popup_take').hide();
		$('.pop_up_bg_take').hide();
		return;
	}
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {
        return;
	}else{
		var ing = layer.load('提交中...');
		this.ajaxRequest_=$.ajax({
		url:"${root}/newsCommentAction_addComment.action",
		type:"POST",
		dataType:"json",
		data:{newsId:newsId,words:words},
		success:function(data){
			var json = eval(data);									
			$.each(json, function (index) { 
				  var flag = json[index].OK;
				  var exception = json[index].exception;
				  var loginFlag = json[index].loginSign;
				  
				  if(loginFlag == "false" || loginFlag == false){
					  layer.close(ing);
					  $('.popup_take').show();
					  $('.pop_up_bg_take').show();
					  return;
				  }else{
					  $('.popup_take').hide();
					  $('.pop_up_bg_take').hide();
				  }
				  layer.close(ing);
				  if (flag == "false" || flag == false) {
					 if($("#userName").val()==null || $("#userName").val()==""){
						 layer.alert("评论失败\n" + exception, 8,function(){
	   						  window.location.reload();
	   					 });
	   				 }else{
	   					layer.alert("评论失败\n" + exception, 8);
	   				 }
	   			  } else {
	   				 if($("#userName").val()==null || $("#userName").val()==""){
	   					 layer.alert("评论成功",9,function(){
	   						  window.location.reload();
	   					 });
	   				 }else{
	   					 layer.alert("评论成功",9);
	   				 }
	   			  }
				 commentListInit("newsCenterAction_getCommentList.action",1,10);
				 $("#newsWords").val(""); 
			});
		}
		});
	}
}

function filter(text) {
	text = text.replace(/\s/g, "");
    text = text.replace(/\"/g, "");
    text = text.replace(/\'/g, "");
    text = text.replace(/\\/g, "");
    text = text.replace(/<[^>]*>/g, "");
    return text;
}

function addCommentRevert(commentId){
	
	var words = filter($("#"+commentId+"commentWords").val());
	if(words==null || words==""){
		layer.alert("回复信息不能为空",8);
		$('.popup_take').hide();
		$('.pop_up_bg_take').hide();
		return;
	}else if(words.length > 300){
		layer.alert("回复信息不能超过300字回复失败。");
		$('.popup_take').hide();
		$('.pop_up_bg_take').hide();
		return;
	}
	
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {
        return;
	}else{
		var ing = layer.load('提交中...');
		this.ajaxRequest_=$.ajax({
		url:"${root}/newsCenterAction_addCommentRevert.action",
		type:"POST",
		dataType:"json",
		data:{commentId:commentId,words:words},
		success:function(data){
			var json = eval(data);									
			$.each(json, function (index) { 
				  var flag = json[index].OK;
				  var exception = json[index].exception;
				  var loginFlag = json[index].loginSign;
				  //验证用户是否登录
				  if(loginFlag == "false" || loginFlag == false){
					  layer.close(ing);
					  $("#commentId").val(commentId);
					  $('.popup_take').show();
					  $('.pop_up_bg_take').show();
					  return;
				  }else{
					  $('.popup_take').hide();
					  $('.pop_up_bg_take').hide();
				  }
				 
				  layer.close(ing);
				  if (flag == "false" || flag == false) {
					  if($("#userName").val()==null || $("#userName").val()==""){
							 layer.alert("回复失败\n" + exception, 8,function(){
		   						  window.location.reload();
		   					 });
		   				 }else{
		   					layer.alert("回复失败\n" + exception, 8);
		   				 }
	   			  } else {
	   				 if($("#userName").val()==null || $("#userName").val()==""){
	   					 layer.alert("回复成功",9,function(){
	   						  window.location.reload();
	   					 });
	   				 }else{
	   					 layer.alert("回复成功",9);
	   				 }
	   			  }
				  $("#"+commentId+"_bigdiv").hide();
				  $("#"+commentId+"_addrevertdiv").hide();
				  var pageNo = $("#pageSign").val();
				  if(pageNo == null || pageNo == ""){
					  pageNo = 1;
				  }
				  commentListInit("newsCenterAction_getCommentList.action",pageNo,10);
				  $("#commentWords").val("");
			});
		}
		});
	}
}

function wordsNum(){
	var num = $("#newsWords").val().length;
	$('#wordsNum').html(num +'/150');
}


/*提交登录数据*/
function checkLogin() {
	var isshowdialogcode='${showDialogCode}';
	//获取用户名
	var username = $("#accName").val();
	//获取密码
	var password = $("#pwd").val();
	//判断用户名不为空，否则返回
	if (username == '' || username.length>32) {
		$("#nameMsg").show();
		return false;
	}
	//判断密码不为空，否则返回
	if (password == '') {
		$("#pwdMsg").show();
		$("#pwdMsg").text("请输入登录密码！");
		return false;
	}
	
	//校验验证码
	if("true"==isshowdialogcode){
		if($("#dialogCode").val()==''){
	  		$("#pwdMsg").show();
				$("#pwdMsg").text("请输入验证码！");
				return false;
		}else if($("#dialogCode").val().length != 4){
	  		$("#pwdMsg").show();
				$("#pwdMsg").text("请输入4位数验证码！");
				return false;
		}
	}
	$("#nameMsg").hide();
	$("#pwdMsg").hide();
	//登录
	$.ajax({
		type : "POST",
		url :'../user/userAction_newsLogin.action',
		//url:"${root}/newsCommentAction_addComment.action",
		data: {"userRegister.accName":username,"userRegister.pwd":password},
		dataType : 'text',
		success : function(data) {        
			if(data == "true"){
				$("#loginForm").hide();//隐藏登录框
				var commId = $("#commentId").val();
				if(commId == "" || commId == null){
					addComment();
				}else{
				  //验证该条新闻是否允许回复
				  var canDiscussValue = $("#canDiscuss").val();
				  if(canDiscussValue != null && canDiscussValue != ""){
					  if(canDiscussValue == 0 || canDiscussValue == '0'){
					  	  $('.popup_take').hide();
				      	  $('.pop_up_bg_take').hide();
		   				  layer.alert("回复失败，该条新闻不允许回复。", 8,function(){
		   						  window.location.reload();
		   				  });
		   				  return;
					  }else{
					 	 addCommentRevert(commId);
					  }
				  }else{
				  	  addCommentRevert(commId);
				  }
				}
			}else {
				$("#pwdMsg").html(data);
				$("#pwdMsg").show();
				return;
			} 
		}
	});
}